package com.example.menu;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class Score extends Activity {
	File localHighScoresFile = null;
	BufferedReader localHighScoresBufferedReader;
	String [] tokens;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score);
		
		openLocalHighScoresFile();
		getHighScores();
		createTable();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.score, menu);

		return true;
	}

	// Use this function to end the score activity, this should bring us back to
	// the main menu
	public void endScore(View view) {
		this.finish();
	}

	public void openLocalHighScoresFile() {
		try {
			if (localHighScoresFile == null) {
				String theFile = "localHighScores";
				File direc = new File(Environment.getExternalStorageDirectory()
						+ "/squared/");
				if (direc.mkdir() || direc.isDirectory()) {

					localHighScoresFile = new File(
							Environment.getExternalStorageDirectory()
									+ "/squared/" + theFile + ".txt");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getHighScores() {
		try {
			localHighScoresBufferedReader = new BufferedReader(new FileReader(
					localHighScoresFile));
			
			String line = localHighScoresBufferedReader.readLine();
			String delims = ";";
			tokens = line.split(delims);
			
		} catch (Exception e) {

		}
	}
	
	public void createTable () {	
		((TextView) findViewById (R.id.tableName1)).setText(tokens[0]);
		((TextView) findViewById (R.id.tableScore1)).setText(tokens[1]);

		((TextView) findViewById (R.id.tableName2)).setText(tokens[2]);
		((TextView) findViewById (R.id.tableScore2)).setText(tokens[3]);
		
		((TextView) findViewById (R.id.tableName3)).setText(tokens[4]);
		((TextView) findViewById (R.id.tableScore3)).setText(tokens[5]);
		
		((TextView) findViewById (R.id.tableName4)).setText(tokens[6]);
		((TextView) findViewById (R.id.tableScore4)).setText(tokens[7]);
		
		((TextView) findViewById (R.id.tableName5)).setText(tokens[8]);
		((TextView) findViewById (R.id.tableScore5)).setText(tokens[9]);
		
		((TextView) findViewById (R.id.tableName6)).setText(tokens[10]);
		((TextView) findViewById (R.id.tableScore6)).setText(tokens[11]);
		
		((TextView) findViewById (R.id.tableName7)).setText(tokens[12]);
		((TextView) findViewById (R.id.tableScore7)).setText(tokens[13]);
		
		((TextView) findViewById (R.id.tableName8)).setText(tokens[14]);
		((TextView) findViewById (R.id.tableScore8)).setText(tokens[15]);
		
		((TextView) findViewById (R.id.tableName9)).setText(tokens[16]);
		((TextView) findViewById (R.id.tableScore9)).setText(tokens[17]);
		
		((TextView) findViewById (R.id.tableName10)).setText(tokens[18]);
		((TextView) findViewById (R.id.tableScore10)).setText(tokens[19]);
	}
}
